package com.kg.module.jtnc.applet.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.core.annotation.AutoOperateLog;
import com.kg.core.annotation.NoRepeatSubmit;
import com.kg.core.exception.BaseException;
import com.kg.module.jtnc.applet.dto.RwdfDTO;
import com.kg.module.jtnc.rbgl.dto.KrRbglDTO;
import com.kg.module.jtnc.rbgl.dto.convert.KrRbglConvert;
import com.kg.module.jtnc.rbgl.service.KrRbglService;
import com.kg.module.jtnc.rghs.dto.KrRghsDTO;
import com.kg.module.jtnc.rghs.dto.convert.KrRghsConvert;
import com.kg.module.jtnc.rghs.entity.KrRghs;
import com.kg.module.jtnc.rghs.service.KrRghsService;
import com.kg.module.jtnc.rwgl.dto.KrRwglDTO;
import com.kg.module.jtnc.rwgl.dto.convert.KrRwglConvert;
import com.kg.module.jtnc.rwgl.entity.KrRwgl;
import com.kg.module.jtnc.rwgl.service.KrRwglService;
import com.kg.module.jtnc.util.PublicUtil;
import com.kg.module.jtnc.zzjh.dto.convert.KrZzjhConvert;
import com.kg.module.jtnc.zzjh.entity.KrZzjh;
import com.kg.module.jtnc.zzjh.service.KrZzjhService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author Sunshuwei
 * date 2024-11-05
 */
@RestController
@RequestMapping("/jtnc/applet/jsy")
@Api(tags = "小程序-技术员接口", value = "小程序-技术员接口", description = "AppletJsyController")
public class AppletJsyController {
    @Resource
    private KrRwglService krRwglService;
    @Resource
    private KrRwglConvert krRwglConvert;
    @Resource
    private KrZzjhService krZzjhService;
    @Resource
    private KrZzjhConvert krZzjhConvert;
    @Resource
    private KrRghsService krRghsService;
    @Resource
    private KrRghsConvert krRghsConvert;
    @Resource
    private KrRbglService krRbglService;
    @Resource
    private KrRbglConvert krRbglConvert;


    @ApiOperation(notes = "/jtnc/applet/jsy/listDkxx", value = "获取技术员管理的地块信息（种植计划启用中的）", httpMethod = "GET")
    @GetMapping("/listDkxx")
    public List<KrZzjh> listDkxx() {
        List<KrZzjh> list = krZzjhService.lambdaQuery()
                .select(KrZzjh::getDkglId, KrZzjh::getDkmc)
                .eq(KrZzjh::getJsyId, PublicUtil.getUserId())
                .eq(KrZzjh::getState, 1)
                .orderByAsc(KrZzjh::getDkmc)
                .list();
        return list;
    }

    /*****************************************************任务*****************************************************/

    @ApiOperation(notes = "/jtnc/applet/jsy/getRwById", value = "详情-任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rwId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getRwById")
    public KrRwglDTO getRwById(String rwId) {
        return krRwglConvert.entityToDto(krRwglService.getById(rwId));
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/listRw", value = "分页列表-任务", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRw")
    public Page<KrRwglDTO> listRw(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                  @RequestParam(value = "params", required = false) String params) {
        JSONObject jsonObject = new JSONObject(params);
        jsonObject.set("zzjhIds", PublicUtil.getZzjhIdsByJsy());
        return krRwglService.pagelist(page, limit, JSONUtil.toJsonStr(jsonObject));
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/addRw", value = "发布任务", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/addRw")
    @NoRepeatSubmit
    public void addRw(@Validated @RequestBody KrRwglDTO krRwglDTO) throws BaseException {
        try {
            krRwglService.add(krRwglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }


    @ApiOperation(notes = "/jtnc/applet/jsy/rwdf", value = "任务打分", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/rwdf")
    @NoRepeatSubmit
    public void rwdf(@Validated @RequestBody RwdfDTO rwdfDTO) throws BaseException {
        try {
            KrRwgl byId = krRwglService.getById(rwdfDTO.getRwId());
            if (!byId.getRwzt().equals("2")) {
                throw new BaseException("只有完成的任务才能打分");
            }
            if (byId.getRwzt().equals("1")) {
                throw new BaseException("任务已打分，请勿重复打分");
            }

            krRwglService.lambdaUpdate()
                    .set(KrRwgl::getFs, rwdfDTO.getFs())
                    .set(KrRwgl::getDfpy, rwdfDTO.getDfpy())
                    .set(KrRwgl::getDfzt, 1)
                    .eq(KrRwgl::getRwId, rwdfDTO.getRwId())
                    .update();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "任务打分失败！请重试");
        }
    }

    /*****************************************************人工核算*****************************************************/

    @ApiOperation(notes = "/jtnc/applet/jsy/getRghsById", value = "详情-人工核算表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rghsId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getRghsById")
    public KrRghsDTO getRghsById(String rghsId) {
        return krRghsConvert.entityToDto(krRghsService.getById(rghsId));
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/listRghs", value = "分页列表-人工核算表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRghs")
    public Page<KrRghsDTO> listRghs(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                    @RequestParam(value = "params", required = false) String params) {
        JSONObject jsonObject = new JSONObject(params);
        jsonObject.set("zzjhIds", PublicUtil.getZzjhIdsByJsy());
        return krRghsService.pagelist(page, limit, JSONUtil.toJsonStr(jsonObject));
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/addRghs", value = "新增-人工核算表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/addRghs")
    @NoRepeatSubmit
    public void addRghs(@Validated @RequestBody KrRghsDTO krRghsDTO) throws BaseException {
        try {
            krRghsService.add(krRghsDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/updateRghs", value = "修改-人工核算表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/updateRghs")
    @NoRepeatSubmit
    public void updateRghs(@Validated @RequestBody KrRghsDTO krRghsDTO) throws BaseException {
        try {
            KrRghs byId = krRghsService.getById(krRghsDTO.getRghsId());
            if (!byId.getState().equals("2")) {
                throw new BaseException("只有拒绝确认的核算才能修改");
            }
            krRghsService.update(krRghsDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "修改失败！请重试");
        }
    }


    /*****************************************************日报**************************************************/
    @ApiOperation(notes = "/jtnc/applet/jsy/getRbById", value = "详情-日报管理", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rbId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getRbById")
    public KrRbglDTO getRbById(String rbId) {
        return krRbglConvert.entityToDto(krRbglService.getById(rbId));
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/listRb", value = "分页列表-日报管理", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRb")
    public Page<KrRbglDTO> listRb(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                  @RequestParam(value = "params", required = false) String params) {
        return krRbglService.pagelist(page, limit, params);
    }

    @ApiOperation(notes = "/jtnc/applet/jsy/addRb", value = "新增(补交)-日报管理", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/addRb")
    @NoRepeatSubmit
    public void addRb(@Validated @RequestBody KrRbglDTO krRbglDTO) throws BaseException {
        try {
            krRbglService.add(krRbglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }

}


